'use strict';

angular.module('app')
    .controller('mainTainDetailsController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster,$modal) {
            $scope.title = '设备报障详情';
            $scope.radio1 = false;
            $scope.param = {};
            $scope.param.maintainId = $state.params.id;//路由传过来的参数
            $scope.loading = false;
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({//页面初始化数据请求
                    type: 'POST',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/dac/dm/DmMaintain/Detials',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                        $scope.photos = JSON.parse(result.data.faultPhoto);
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.search();
            
            var img = $('#bigerImg');
            //鼠标移入图片上
            $scope.scalImg = function(m_id,even){
                var src = 'https://udacs.gmcciot.com:9527/app-api/android/download?mediaId='+m_id;
                var e = event || window.event;
                var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;//获取页面的水平滚动条的位置
                var scrollY = document.documentElement.scrollTop || document.body.scrollTop;//获取页面的垂直滚动条的位置
                //console.log(scrollX,scrollY);//不出意外是(0,0);
                var x = (e.pageX || e.clientX + scrollX);//e.pageX是鼠标距离页面左边的位置
                var y = (e.pageY || e.clientY);//e.pageY是鼠标距离页面顶部的位置
				//console.log(x,y);
                img.attr('src',src).css({'max-width':'30%','position':'absolute','left':x+150+'px','top':y-150+'px','z-index':'9'});
            }
            //鼠标在图片上移动
            $scope.move = function(event){
                var e = event || window.event;
                var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
                var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
                var x = e.pageX || e.clientX + scrollX;
                var y = e.pageY || e.clientY;
                img.css({'position':'absolute','left':x+150+'px','top':y-150+'px'});
            }
            //鼠标移除图片
            $scope.out = function(event){
                img.attr('src','');
            }
			//确认信息
			$scope.confirm = function(){
				$scope.data = {
					isSolve  : $scope.pageInfo.isSolve ,
					groupCode : $scope.pageInfo.groupCode,
					handleResult : $scope.pageInfo.handleResult,
					id:$state.params.id
				};
				console.log($scope.data);
				($scope.data.isSolve)? save():alert('问题是否已解决了？');
				function save(){
					$scope.loading = true;
					$.ajax({
						type:"post",
						url:"/dac/dm/DmMaintain/confirm",
						async:true,
						dataType: 'json',
                    	contentType:'application/json;charset=UTF-8',
                    	data:angular.toJson($scope.data)
					}).then(function(res){
						console.log(res);
						$scope.loading = false;
						if(res.httpCode==200){
							$timeout(function(){
                        		$state.go('main.dm.DmMaintain.list')
                        	},2000)
						}else{
							toaster.clear('*');
							toaster.pop('error','',res.msg);
						}
					})
				}
				
			}
			//维修组模态框
            $scope.openGroup = function() {
              
                var modalInstance = $modal.open({
                    templateUrl : 'MainGroupList.html',// script标签中定义的id
                    controller : 'MainGroupController',// modal对应的Controller
                    size: 'lg',
                    resolve : {
                        items : function() {
                            return '';
                        }
                    }
                });

                modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数
		
                });
                
                modalInstance.result.then(function(item) {
					console.log(item);
					$scope.pageInfo.groupName = item.groupName;
					$scope.pageInfo.groupCode = item.groupCode;
                }, function(reason) {
                    console.log(reason)
                });
            };
            
            //选择组模态框控制器
	        app.controller('MainGroupController', function($scope, $rootScope, $modal,$modalInstance,items) {
	              	$scope.param ={};
	                $scope.search = function () {
	                    $scope.loading = true;
	                    if($scope.pageFlag != 1){//未点击翻页
	                        $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
	                    }else{//点击翻页
	                        $scope.pageFlag = 2;//将开关值设置为2
	                    }
	                    $.ajax({
	                        type: 'POST',
	                        dataType: 'json',
	                        contentType:'application/json;charset=UTF-8',
	                        url : '/dac/dm/DmMaintain/groupList',
	                        data: angular.toJson($scope.param)
	                    }).then(function(result) {
	                        $scope.loading = false;
	                        if (result.httpCode == 200) {
	                            $scope.pageInfo = result;
	                        } else {
	                            toaster.clear('*');
	                            toaster.pop('error', '', result.msg);
	                        }
	                        $scope.$apply();
	                    });
	                }
	                $scope.search();
	               
				//选择维修组
				 $scope.selectGroup = function(groupName,groupCode){
            		var item = {
	            		groupName:groupName,
	            		groupCode:groupCode
            		};
            		
            		$modalInstance.close(item);
	            }
                $scope.cancel = function() {
                    $modalInstance.dismiss('cancel');
                };

                // 翻页
                $scope.pagination = function (page) {
                    $scope.param.pageNum=page;
                    $scope.pageFlag = 1;//将开关值设置为1
                    $scope.search();
                };
            })

} ]);